﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XinYi.CrookedShowMode;

namespace XinYi.CrookedShow.BLL
{
    public class Product_SpuService : BaseService<Product_Spu>
    {
        public List<ProductsInfo> GetRoleList()
        {
            //创建List对象
            List<ProductsInfo> adminUserQueries = new List<ProductsInfo>();
            //创建实例
            using (var db = new CrookedShowEntities())
            {
                //select b.Id,b.Logo,b.Name,COUNT(BrandId) as Counts from dbo.Brand as b left join dbo.Product_Spu as p on b.Id 
                //    = p.BrandId group by b.Id,b.Logo,b.Name

                //select p.Id,Code,c.Name,p.Name,Price,InDate,Audit_Status,Publish_Status from Product_Spu as p inner join dbo.Category as c
                //on p.One_Category_Id = c.Id

                var result = from a in db.Product_Spu
                             join b in db.Category
                             on a.One_Category_Id.ToString() equals b.Id.ToString()
                             select new ProductsInfo()
                             {
                                 Id = a.Id,
                                 Code = a.Code,
                                 Name1 = b.Name,
                                 Name2 = a.Name,
                                 Price = a.Price,
                                 InDate = a.InDate,
                                 Audit_Status = a.Audit_Status,
                                 Publish_Status = a.Publish_Status
                             };
                             //from ab in aa.DefaultIfEmpty()
                             //group new { a, ab } by new { a.Id, a.Name, a.Logo, a.Status, a.UpdateTime } into e
                             //select new BrandInfo()
                             //{
                             //    Id = e.Key.Id,
                             //    Name = e.Key.Name,
                             //    Logo = e.Key.Logo,
                             //    Status = e.Key.Status,
                             //    UpdateTime = e.Key.UpdateTime.Value,
                             //    Counts = e.FirstOrDefault().ab.One_Category_Id == null ? 0 : e.Count()
                             //};
                return result.ToList();
            }
        }

        public bool AddProduct_SpuInfo(AddProduct_Spu addProduct_Spu)
        {
            Product_Spu product = new Product_Spu();//商品信息表
            Product_Sku sku = new Product_Sku();//库存
            //为商品表赋值
            product.Code = addProduct_Spu.Code;
            product.Name = addProduct_Spu.Product_SpuName;
            product.BrandId = addProduct_Spu.BrandId;
            product.One_Category_Id = addProduct_Spu.One;
            product.Two_Category_Id = addProduct_Spu.Two;
            product.Three_Category_Id = addProduct_Spu.Three;
            product.Price = addProduct_Spu.Price;
            product.Publish_Status = addProduct_Spu.Publish_Status;
            product.Audit_Status = addProduct_Spu.Audit_Status;
            product.Weight = addProduct_Spu.Weight;
            product.Production_Date = DateTime.Now;  //页面没生产日期，随便弄的
            product.Shelf_Life = DateTime.Now;       //页面没产品有效期,随便弄的
            product.Descript = addProduct_Spu.Descript;
            product.InDate = DateTime.Now;
            product.ModifiedTime = DateTime.Now;
            //为库存表赋值
            sku.Stock = addProduct_Spu.Stock;
            sku.Spec_Id = 0;  //规格Id,页面没做出，所以定死的
            sku.CreateTime = DateTime.Now;
            sku.ModifiedTime = DateTime.Now;
            /*ProductId int NOT NULL,--商品Id*/
            //创建实例
            using (var db = new CrookedShowEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //添加商品表
                        db.Product_Spu.Add(product);
                        //保存到数据库
                        db.SaveChanges();

                        //添加第二表拿到上面添加数据的外键ID赋值
                        sku.ProductId = product.Id;
                        //添加第二章表
                        db.Product_Sku.Add(sku);
                        db.SaveChanges();
                        //事务提交
                        tran.Commit();
                        return true;
                    }
                    catch
                    {
                        //报错回滚事务
                        tran.Rollback();
                        return false;
                    }
                }
            } 
        }
    }
}
